package de.tudarmstadt.fgbs.cafe.structure;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Class, which represents a station (ger.: Bahnhof)
 * 
 * @author Chris
 * @version 0.2
 * 
 */

public class Bahnhof implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 4034201946873190410L;

    /**
     * Variable for the available tracks
     */
    private List<String> gleise;
    /**
     * List for storing the neighbor stations
     */
    private List<Bahnhof> nachbarn;
    /**
     * String containing the name
     */
    private String name;
    /**
     * String containing the short name
     */
    private String kurzbez;
    /**
     * Bahnhof for representing a associated haltepunkt
     */
    private Bahnhof haltepunkt;

    private boolean isHaltepunkt = false;

    private boolean isBlockstelle = false;

    /**
     * Constructor for the Bahnhof Initializes the lists for neighbors and
     * tracks
     */
    public Bahnhof() {
	gleise = new ArrayList<String>();
	nachbarn = new ArrayList<Bahnhof>();
    }

    /**
     * Returns a list of available tracks
     * 
     * @return a list of strings containing the tracks
     */
    public List<String> getGleise() {
	return gleise;
    }

    /**
     * Set a new list of tracks
     * 
     * @param gleise
     *            list of strings containing the tracks
     */
    public void setGleise(List<String> gleise) {
	this.gleise = gleise;
    }

    /**
     * Retrieve the neighbors of the station
     * 
     * @return list of neighboring stations
     */
    public List<Bahnhof> getNachbarn() {
	return nachbarn;
    }

    /**
     * Set new neighboring stations
     * 
     * @param nachbarn
     *            a list of neighboring stations
     */
    public void setNachbarn(List<Bahnhof> nachbarn) {
	this.nachbarn = nachbarn;
    }

    /**
     * Returns the long name of the stations
     * 
     * @return the long name
     */
    public String getName() {
	return name;
    }

    /**
     * Set the long name of the station
     * 
     * @param name
     *            the long name of the station
     */
    public void setName(String name) {
	this.name = name;
    }

    /**
     * Retrieve the short name of the station
     * 
     * @return the short name of the station
     */
    public String getKurzbez() {
	return kurzbez;
    }

    /**
     * Set a new short name for the station
     * 
     * @param kurzbez
     *            the short name of the station
     */
    public void setKurzbez(String kurzbez) {
	this.kurzbez = kurzbez;
    }

    /**
     * Method for adding a neighboring Bahnhof to the current one Note: A
     * two-way relation is created, this means, that the current Bahnhof is also
     * set as neighbor for the given neighbor
     * 
     * @param newNachbar
     *            Bahnhof, which should be added
     */
    public void addNachbar(Bahnhof newNachbar) {
	if (!nachbarn.contains(newNachbar)) {
	    nachbarn.add(newNachbar);
	    newNachbar.addNachbar(this);
	}
    }

    /**
     * Method for resolving a string representation of all available track
     * numbers
     * 
     * @return String consisting of the comma delimited track numbers
     */
    public String getGleiseAsString() {
	StringBuffer ausgabe = new StringBuffer();
	for (Iterator<String> it = gleise.iterator(); it.hasNext();) {
	    String current = it.next();
	    ausgabe.append(current);
	    if (it.hasNext())
		ausgabe.append(",");
	}
	return ausgabe.toString();
    }

    /**
     * Method for retrieving an according Haltepunkt for this station
     * 
     * @return the according Haltepunk or null, if no Haltepunkt is available
     */
    public Bahnhof getHaltepunkt() {
	return haltepunkt;
    }

    /**
     * Set a new Haltepunkt for the Bahnhof
     * 
     * @param haltepunkt
     *            the Haltepunkt to be set
     * 
     * @see Bahnhof
     */
    public void setHaltepunkt(Bahnhof haltepunkt) {
	this.haltepunkt = haltepunkt;
    }

    /**
     * @return the isHaltepunkt
     */
    public boolean isHaltepunkt() {
	return isHaltepunkt;
    }

    /**
     * @param isHaltepunkt
     *            the isHaltepunkt to set
     */
    public void setIsHaltepunkt(boolean isHaltepunkt) {
	this.isHaltepunkt = isHaltepunkt;
    }

    /**
     * @return the isBlockstelle
     */
    public boolean isBlockstelle() {
	return isBlockstelle;
    }

    /**
     * @param isBlockstelle
     *            the isBlockstelle to set
     */
    public void setBlockstelle(boolean isBlockstelle) {
	this.isBlockstelle = isBlockstelle;
    }

    public boolean hasNeighbor(Bahnhof candidate) {
	for (Iterator<Bahnhof> it = this.nachbarn.iterator(); it.hasNext();) {
	    if (candidate.getKurzbez().equals(it.next().kurzbez))
		return true;
	}
	return false;
    }

    @Override
    public String toString() {
	return this.kurzbez + " (" + this.name + ")";
    }

}
